R Tutorials by : Er Karan Arora : Founder & CEO - ITRONIX SOLUTIONS

List is a data structure having components of mixed data types.

A vector having all elements of the same type is called atomic vector but a vector having elements of different type is called list.

We can check if it’s a list with typeof() function and find its length using length().

In [2]:
L=list(10,3.45,"Itronix",56,3.45,"Solutions")
L
  1. 10
  2. 3.45
  3. 'Itronix'
  4. 56
  5. 3.45
  6. 'Solutions'
In [4]:
L[1]
L[2]
  1. 10
  1. 3.45
In [25]:
# Add element at the end of the list.
L[4] = "Karan Arora"
print(L[4])

# Remove the last element.
L[4] = NULL

# Print the 4th Element.
print(L[4])

# Update the 3rd Element.
L[3] = "iTronix"
print(L[3])
[[1]]
[1] "Karan Arora"

[[1]]
[1] 3.45

[[1]]
[1] "iTronix"

In [26]:
print(L)
[[1]]
[1] 10

[[2]]
[1] 3.45

[[3]]
[1] "iTronix"

[[4]]
[1] 3.45

[[5]]
[1] "Solutions"

In [5]:
L1=list(1,2,3)
L2=list(4,5,6)
Merge=c(L1,L2)
Merge
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6

Converting List to Vector

A list can be converted to a vector so that the elements of the vector can be used for further manipulation. All the arithmetic operations on vectors can be applied after the list is converted into vectors. To do this conversion, we use the unlist() function. It takes the list as input and produces a vector.

In [9]:
list1 = list(1:5)
list1

list2 =list(10:14)
list2

# Convert the lists to vectors.
v1 = unlist(list1)
v2 = unlist(list2)

v1
v2

# Now add the vectors
result = v1+v2
result
    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    1. 10
    2. 11
    3. 12
    4. 13
    5. 14
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  1. 10
  2. 11
  3. 12
  4. 13
  5. 14
  1. 11
  2. 13
  3. 15
  4. 17
  5. 19
In [ ]:

In [ ]: